using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using System.IO;

namespace SgsUI
{
    [RequireComponent(typeof(RectTransform))]
    [RequireComponent(typeof(SpriteRes))]
    [RequireComponent(typeof(ButtonRes))]

    public class ViewButton : ViewNode
    {
        public ButtonRes.Type button_type;

        public string res;

        public string normal;

        public string pressed;

        public string disabled;

        private ButtonRes buttonRes;
        private ButtonRes m_buttonRes
        {
            set{buttonRes = value;}
            get
            {
                if(buttonRes == null)
                {
                    if(TryGetComponent<ButtonRes>(out ButtonRes button))
                        buttonRes = button;
                }
                    
                return buttonRes;
            }
        }

        public override void InitViewNode()
        {
            base.InitViewNode();
            if(TryGetComponent<ButtonRes>(out ButtonRes button) && TryGetComponent<SpriteRes>(out SpriteRes sprite))
            {
                button.m_sprite = sprite;
            }
        }

        protected override string GetDefaultName()
        {
            return "button";
        }

        public override bool HasAtriSize()
        {
            return false;
        }

        public void UpdateButton()
        {
            if(m_buttonRes == null)
                return;

            button_type = m_buttonRes.m_type;

            if(button_type == ButtonRes.Type.Simple_Btn)
            {
                type = "simple_btn";
                res = m_buttonRes.res;
            }
            else if(button_type == ButtonRes.Type.Normal_Btn)
            {
                type = "normal_btn";
                normal = m_buttonRes.m_ButtonRes.sprite_normal;

                if(m_buttonRes.m_ButtonRes.sprite_pressed)
                {
                    pressed = "#" + m_buttonRes.m_ButtonRes.sprite_pressed.name + ".png";
                }
                else
                {
                    pressed = String.Empty;
                }

                if(m_buttonRes.m_ButtonRes.sprite_disabled)
                {
                    disabled = "#" + m_buttonRes.m_ButtonRes.sprite_disabled.name + ".png";
                }
                else
                {
                    disabled = String.Empty;
                }
            }
            else if(button_type == ButtonRes.Type.Common_Btn)
            {
                type = "cmn_btn";
            }
        }

        protected override void WriteOtherParam(NodeData nodeData)
        {
            if(button_type == ButtonRes.Type.Simple_Btn)
            {
                if(res != String.Empty)
                {
                    ParamData param_res = new ParamData();
                    param_res.SetValue("res",res);
                    nodeData.AddParam(param_res);
                }
            }
            else if(button_type == ButtonRes.Type.Normal_Btn)
            {
                if(normal != String.Empty)
                {
                    ParamData param_normal = new ParamData();
                    param_normal.SetValue("normal",normal);
                    nodeData.AddParam(param_normal);
                }

                if(pressed != String.Empty)
                {
                    ParamData param_pressed = new ParamData();
                    param_pressed.SetValue("pressed",pressed);
                    nodeData.AddParam(param_pressed);
                }

                if(disabled != String.Empty)
                {
                    ParamData param_disabled = new ParamData();
                    param_disabled.SetValue("disabled",disabled);
                    nodeData.AddParam(param_disabled);
                }
            }
        }
    }
}